# 我农场贼乱耍金币脚本
from mitmproxy import ctx, http
import random
import re

# def request(flow: http.HTTPFlow) -> None:
#     ctx.log.error(f"path={flow.request.path}")
#     if flow.request.path == "/practice1/user/beatHeart": 
#         content = flow.request.get_content()
#         ctx.log.error(f'1.{content} {len(content)}')
#         content = re.sub(br'"money": \d+\.\d+', f'"money": {random.randint(700000, 800000)}'.encode(), content)
#         flow.request.set_content(content)
#         ctx.log.error(f'2.{content} {len(content)}')

def request(flow: http.HTTPFlow) -> None:
    ctx.log.error(f"path={flow.request.path}")
    content = flow.request.get_content()
    if content.find(b"money") != -1:
        content = re.sub(br'"money": \d+\.\d+', f'"money": {random.randint(50000000, 51000000)}'.encode(), content)
    if content.find(b"isAd") != -1:
        content = re.sub(br'"isAd": (false|true)', f'"isAd": true'.encode(), content)
    flow.request.set_content(content)